/**
 * @(#)HistoryDataMan.java, 2014年10月13日. Copyright 2012 Yodao, Inc. All rights
 * reserved. YODAO PROPRIETARY/CONFIDENTIAL. Use is
 * subject to license terms.
 */
package com.scitc.pleasurevideo.data;

import android.content.Context;

import java.util.ArrayList;
import java.util.List;

import static com.scitc.pleasurevideo.data.ETMan.getPref;
import static com.scitc.pleasurevideo.data.ETMan.savePref;


/**
 * @author lukun 解析数据为对象
 */
public class HistoryDataMan {

    private static HistoryDataMan historyDataMan;

    private HistoryDataMan() {
        super();
    }

    public synchronized static HistoryDataMan getHistoryDataMan() {
        if (historyDataMan == null) {
            historyDataMan = new HistoryDataMan();
        }
        return historyDataMan;
    }


    public List<String> getHistoryQueryKeywords(Context context) {
        String json = getPref(context, "config_querykeywords", "[]");
        List<String> words = ETMan.getMananger().getGson().fromJson(json, List.class);
        List<String> results = new ArrayList<String>();
        for (int i = words.size() - 1; i >= 0; i--) {
            results.add(words.get(i));
        }

        return results;
    }

    public void addHistoryQueryKeyword(Context context, String s) {
        String json = getPref(context, "config_querykeywords", "[]");
        List<String> keywords = ETMan.getMananger().getGson().fromJson(json, List.class);
        keywords.remove(s);
        keywords.add(s);
        if (keywords.size() > 10) {
            keywords.remove(0);
        }
        json = ETMan.getMananger().getGson().toJson(keywords);
        savePref(context, "config_querykeywords", json);
    }

    public void clearHistoryQueryKeyword(Context context) {
        savePref(context, "config_querykeywords", "[]");
    }

    public void clearHistoryQueryString(Context context, String s) {
        String json = getPref(context, "config_querykeywords", "[]");
        List<String> keywords = ETMan.getMananger().getGson().fromJson(json, List.class);
        keywords.remove(s);
        json = ETMan.getMananger().getGson().toJson(keywords);
        savePref(context, "config_querykeywords", json);
    }
}
